package com.headStorm.guava.manyThread;

import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture;
import com.google.common.util.concurrent.ListeningExecutorService;
import com.google.common.util.concurrent.MoreExecutors;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class ListenableFutureDemo implements Callable<String> {

    String info;
    public ListenableFutureDemo(String info) {
        this.info = info;
    }

    @Override
    public String call() throws Exception {


        while (true){
            System.out.println(Thread.currentThread().getName()+"aaa");
            if("info".equals(info)){
                System.out.println("aa");
            }
        }
    }

    public static void main(String[] args) {
        ExecutorService pool = Executors.newFixedThreadPool(10);
        //包装成Listenable的线程池
        ListeningExecutorService executorService = MoreExecutors.listeningDecorator(pool);
        ListenableFuture<String> future1 = executorService.submit(new ListenableFutureDemo("昨夜西风凋碧树"));
        ListenableFuture<String> future2 = executorService.submit(new ListenableFutureDemo("独上高楼"));

        Futures.addCallback(future1, new CallBackDemo(), MoreExecutors.directExecutor());
        Futures.addCallback(future2, new CallBackDemo(),  pool);

    }
}
